package com.yzr.common.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.logging.Logger;

/**
 * @description 字符流读写
 * @author: yx-0173
 * @date: 2021-05-24 14:01
 **/
public class CharacterIoUtil {
    private static final Logger log = Logger.getLogger("com.yzr.io.CharacterIoUtil");
    private static Integer len;
    private static final char[] BYTES = new char[1024];
    private static boolean result = false;
    private static String stringContent = null;

    /**
     * 将字符串写入文件
     *
     * @param character 字符串
     * @param filePath  文件绝对路径
     * @return 是否成功
     */
    public static boolean characterWrite(String character, String filePath) {
        try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filePath))) {
            if (character.length() > 0) {
                bufferedWriter.write(character);
                result = true;
            }
        } catch (Exception e) {
            log.info("错误...");
        }
        return result;
    }

    /**
     * 从一个文件写入到另一个文件
     *
     * @param fromPath 原文件
     * @param toPath   目标文件
     */
    public static boolean fileWrite(String fromPath, String toPath) {
        try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(toPath));
             BufferedReader bufferedReader = new BufferedReader(new FileReader(fromPath))) {
            while ((len = bufferedReader.read(BYTES)) != -1) {
                bufferedWriter.write(BYTES);
                result = true;
            }
        } catch (Exception e) {
            log.info("错误...");
        }
        return result;
    }

    /**
     * 读取文件中内容，以字符串的形式返回
     *
     * @param filePath 文件路径
     * @return 读取的文件内容信息
     */
    public static String fileRead(String filePath) {
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            while ((len = reader.read(BYTES)) != -1) {
                stringContent = new String(BYTES, 0, len);
            }
        } catch (Exception e) {
            log.info("错误...");
        }
        return stringContent;
    }

    /**
     * 读取文件中内容，以字符串的形式返回
     *
     * @param stream 流对象
     * @return 读取的文件内容信息
     */
    public static String readByStream(InputStream stream) {
        try (InputStreamReader reader = new InputStreamReader(stream, "GBK")) {
            while ((len = reader.read(BYTES)) != -1) {
                stringContent = new String(BYTES, 0, len);
            }
        } catch (Exception e) {
            log.info("错误...");
        }
        return stringContent;
    }

    /**
     * 将流中的字符写入文件
     *
     * @param output 写流对象
     * @param input  读流对象
     */
    public static boolean writeByStream(InputStream input, OutputStream output) {
        try (OutputStreamWriter writer = new OutputStreamWriter(output, "GBK");
             InputStreamReader reader = new InputStreamReader(input, "GBK")) {
            while ((len = reader.read(BYTES)) != -1) {
                writer.write(BYTES);
                result = true;
            }
        } catch (Exception e) {
            log.info("错误...");
        }
        return result;
    }

}
